package com.yxhl.zoume.core.busticket.ui.adapter;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.yxhl.protobuf.ScheduleType;
import com.yxhl.protobuf.YxScheduleEntry;
import com.yxhl.zoume.R;
import com.yxhl.zoume.common.adapter.BaseAdapter;
import com.yxhl.zoume.core.busticket.ui.info.BusHotType;
import com.yxhl.zoume.data.rxbus.RxBus;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Xboy on 16/4/11.
 */
public class BusServicesAdapter extends BaseAdapter<BusServicesAdapter.BusServicesViewHolder> {
    private List<YxScheduleEntry> yxScheduleEntryList;
    private Context context;

    public BusServicesAdapter(List<YxScheduleEntry> busServices, Context context) {
        this.yxScheduleEntryList = busServices;
        this.context = context;
    }

    @Override
    public BusServicesViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View rootView = LayoutInflater.from(context).inflate(R.layout.item_bus_service, parent, false);
        return new BusServicesViewHolder(rootView);
    }

    @Override
    public void onBindViewHolder(BusServicesViewHolder holder, int position) {
        holder.onBindBusService(yxScheduleEntryList.get(position));
    }

    @Override
    public int getItemCount() {
        return yxScheduleEntryList.size();
    }


    public class BusServicesViewHolder extends BaseAdapter.BaseViewHolder {
        private static final String KEY_TAG = "BusServicesViewHolder";

        @BindView(R.id.rl_item_bus_service_root)
        RelativeLayout busServiceRootRl;
        // 流水班icon
        @BindView(R.id.running_class_imageview)
        ImageView mRunningClassImageView;
        @BindView(R.id.tv_item_bus_service_flow_describe)
        TextView busServiceFLowDescribeTv;
        // 发车时间
        @BindView(R.id.departure_time_textview)
        TextView mDepartureTimeTextView;
        // 出发站
        @BindView(R.id.start_station_textview)
        TextView mStartStationTextView;
        // 终点站
        @BindView(R.id.terminal_station_textview)
        TextView mTerminalStationTextView;
        // 票价
        @BindView(R.id.ticket_price_textview)
        TextView mTicketPriceTextView;
        // 余票
        @BindView(R.id.ticket_remaining_textview)
        TextView mTicketRemainingTextView;


        public BusServicesViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        @OnClick(R.id.rl_item_bus_service_root)
        void onItemClick() {
            YxScheduleEntry yxScheduleEntry = yxScheduleEntryList.get(getLayoutPosition());
            RxBus.getInstance().send(yxScheduleEntry);
        }


        public void onBindBusService(YxScheduleEntry busService) {
            // 流水班次icon和文字
            String hot = busService.getHot();
            switch (hot) {
                case BusHotType.FLOW_BUS:
                    mRunningClassImageView.setVisibility(View.VISIBLE);
                    busServiceFLowDescribeTv.setVisibility(View.VISIBLE);
                    break;
                default:
                    mRunningClassImageView.setVisibility(View.GONE);
                    busServiceFLowDescribeTv.setVisibility(View.GONE);
            }
            // 发车时间
            switch (busService.getType()) {
                case V_DIY_BUS:
                    mDepartureTimeTextView.setText("定制\n巴士");
                    mDepartureTimeTextView.setTextColor(ContextCompat.getColor(context, R.color.app_text_blue));
                    break;
                default:
                    mDepartureTimeTextView.setText(busService.getGmtDepartTime());
                    mDepartureTimeTextView.setTextColor(ContextCompat.getColor(context, R.color.app_text_time));
                    break;
            }

            // 始发站
            mStartStationTextView.setText(busService.getStartStationName());
            // 终点站
            mTerminalStationTextView.setText(busService.getEndStationName());
            // 票价
            mTicketPriceTextView.setText("¥" + busService.getTicketPrice());
            // 余票
            if (ScheduleType.V_DIY_BUS == busService.getType()) {
                mTicketRemainingTextView.setText("随时出行");
            } else {
                int numberRemaining = Integer.parseInt(busService.getFreeSeat());
                String numberOfRemainingTickets = "剩余" + numberRemaining + "张票";
                mTicketRemainingTextView.setText(numberRemaining > 10 ? "有票" : (numberRemaining > 0 ? numberOfRemainingTickets : "无票"));
            }
        }


    }
}
